package com.vshop.framework.core.util.validate;


import com.vshop.framework.core.spring.SpringContextUtil;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Set;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidatorBeanUtil {

    private static final Validator VALID = SpringContextUtil.getBean(Validator.class);

    /**
     * 基于Bean中的validator注解校验
     */
    public static <T> void validate(T object, Class<?>... groups) {
        Set<ConstraintViolation<T>> validate = VALID.validate(object, groups);
        if (!validate.isEmpty()) {
            throw new ConstraintViolationException("参数校验异常", validate);
        }
    }
}
